/*
 * Rhy RSS
 * 
 * @author Tristan Maindron
 * @dependencies rhy.core.ajax
 * 
 */

Rhy.extra.rss = {
    channels: [],
    channelsMap: [],
    loadQueue: [],
    synchronous: true,
    loading: false,
    
    onReady: function(){},
    
    reset: function(){
        Rhy.extra.rss.channelsMap = [];
    },
    
    load: function(url, id, callback){
        
        if(typeof(callback) == 'undefined'){
            callback = function(){};
        }
        
        Rhy.extra.rss.loadQueue.push({
            url: url, 
            id: id,
            callback: callback
        });
        
        if(!Rhy.extra.rss.loading || !Rhy.extra.rss.synchronous){
            Rhy.extra.rss.startRequest();
        }
    },
    
    startRequest: function(){
        Rhy.extra.rss.loading = true;
        var o = Rhy.extra.rss.loadQueue.shift();
        
        Rhy.ajax({
          url: o.url,
          type: 'xml',
          success: Rhy.extra.rss.handleSuccess(o.callback, o.id)
        });
    },
    
    handleSuccess: function(callback, id){
        return function(o){
            var channels = o.responseXML.getElementsByTagName('channel');
            for(var i = 0; i < channels.length; i++){

                var elements = [];
                var elementsData = channels[i].getElementsByTagName('item');

                for(var j = 0; j < elementsData.length; j++){
                    if(typeof(elementsData[j].getElementsByTagName('title')[0].firstChild.nodeValue) != 'undefined'){

                        var description = "";

                        if(typeof(elementsData[j].getElementsByTagName('description')[0].firstChild) != 'undefined'
                                && elementsData[j].getElementsByTagName('description')[0].firstChild != null){
                                    description = elementsData[j].getElementsByTagName('description')[0].firstChild.nodeValue.replace(/<\/?[^>]+(>|$)/g, "");
                                }

                        elements.push({
                            title: elementsData[j].getElementsByTagName('title')[0].firstChild.nodeValue,
                            link: elementsData[j].getElementsByTagName('link')[0].firstChild.nodeValue,
                            description: description
                        });
                    }
                    else{
                        elements.push({
                            title: elementsData[j].getElementsByTagName('title')[0].textContent,
                            link: elementsData[j].getElementsByTagName('link')[0].textContent,
                            description: elementsData[j].getElementsByTagName('description')[0].nodeValue.replace(/<\/?[^>]+(>|$)/g, "")
                        });
                    }
                }

                Rhy.extra.rss.channels.push ({
                    title: channels[i].getElementsByTagName('title')[0].textContent,
                    content: elements
                });

                Rhy.extra.rss.channelsMap[id] = {
                    title: channels[i].getElementsByTagName('title')[0].textContent,
                    content: elements
                };
            }

            callback(o);

           Rhy.extra.rss.loading = false;
           if(Rhy.extra.rss.loadQueue.length > 0)
               Rhy.extra.rss.startRequest();
           else{
               Rhy.extra.rss.onReady();
           }
        }
    }
}
